package com.itboye.hutouben.activity.qianbao;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.hutouben.R;
import com.itboye.hutouben.base.BaseActivity;
import com.itboye.hutouben.base.MyApplcation;
import com.itboye.hutouben.bean.BankCodeBean;
import com.itboye.hutouben.presenter.SystemMessgeAPresenter;
import com.itboye.hutouben.presenter.UserPresenter;
import com.itboye.hutouben.util.ByAlert;
import com.itboye.hutouben.util.Const;
import com.itboye.hutouben.util.IsUtilUid;
import com.itboye.hutouben.util.SPUtils;
import com.itboye.hutouben.volley.ResultEntity;

import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * 添加银行卡2
 */
public class AddBankCardTwoActivity extends BaseActivity implements Observer {
    TextView add_shap_title_tv;
    ImageView close_icon;
    EditText add_bank02_phone;
    TextView add_bank02_ok;
    String name;
    String xingming;
    UserPresenter userPresenter;

    SystemMessgeAPresenter presenter;
    List<BankCodeBean> typebean;

    LinearLayout kaihuhang;
    EditText yinhangka_type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_bank_card_two);
        add_shap_title_tv.setText(R.string.bind_yinhangka);
        userPresenter = new UserPresenter(this);
        presenter = new SystemMessgeAPresenter(this);
        String phone = SPUtils.get(MyApplcation.ctx, null, Const.MOBILE, "") + "";
        name = getIntent().getStringExtra("name");
        xingming= getIntent().getStringExtra("xingming");
        add_bank02_phone.setText(phone);

        // 当添加银行卡成功之后
        IntentFilter filter1 = new IntentFilter("tianjia.exit");
        registerReceiver(exitBroadcastReceiver, filter1);
    }

    BroadcastReceiver exitBroadcastReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            finish();
        }
    };


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(exitBroadcastReceiver);
    }

    @Override
    protected void onInit() {

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            case R.id.close_icon:
                finish();
                break;
            case R.id.add_bank02_ok:

                if (add_bank02_phone.getText().toString().equals("")) {
                    ByAlert.alert("请填写手机号码");
                    return;
                }
                if (yinhangka_type.getText().toString().equals("")) {
                    ByAlert.alert("请输入开户行");
                    return;
                }
                showProgressDialog("请稍后", true);
                userPresenter.sendVerificationCode("+86", add_bank02_phone.getText().toString(), 2 + "");

                break;
            default:
                break;
        }
    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity = handlerError(data);
        if (entity != null) {
            IsUtilUid.tianjia(entity);
            if (entity.getEventType() == UserPresenter.send_code_success) {
                ByAlert.alert(entity.getData());
                Intent intent = new Intent(this, AddBankCardThreeActivity.class);
                intent.putExtra("name", name);
                intent.putExtra("xingming", xingming);
                intent.putExtra("yhk_type", yinhangka_type.getText().toString());
                intent.putExtra("phone", add_bank02_phone.getText().toString());
                startActivity(intent);
                finish();

            } else if (entity.getEventType() == UserPresenter.send_code_fail) {
                ByAlert.alert(entity.getData());
            }
        }
        try {
            closeProgressDialog();
        } catch (Exception e) {
        }
    }
}
